package cn.hill4j.tool.spring.ext.alias;

import cn.hill4j.tool.spring.ext.util.SpringBeanUtils;
/**
  * ClassName: BeansAliasUtils
  * Description: springbean 扩展别名工具类
  * @author hillchen
  * Date 2023/8/8
  * @version: 1.0
  */
public class BeansAliasUtils {
    /**
     * springbean 扩展别名默认分组：def
     */
    public static final String DEF_GROUP_NAME="def";
    private static String ALIAS_BEAN_NAME_TEMP = "#A:%s:%s";
    /**
     * @author hillchen
     * Description: 构建springbean别名
     * Date 2023/8/8 23:18
     * @param groupName 分组名称
     * @param alias 别名名称
     * @return String
     **/
    public static String createAliasBeanName(String groupName,String alias){
        return String.format(ALIAS_BEAN_NAME_TEMP,groupName,alias);
    }

    /**
     * @author hillchen
     * Description: 根据别名信息获取springbean
     * Date 2023/8/8 23:20
     * @param <T> springbean 类型
     * @param groupName pringbean别名分组
     * @param alias springbean别名
     * @param clazz springbean类型
     * @return T
     **/
    public static <T> T getBean(String groupName,String alias,Class<T> clazz){
        return SpringBeanUtils.getBean(createAliasBeanName(groupName,alias),clazz);
    }
    /**
     * @author hillchen
     * Description: 根据别名信息获取springbean
     * Date 2023/8/8 23:21
     * @param <T> springbean 类型
     * @param groupName pringbean别名分组
     * @param alias springbean别名
     * @return T
     **/
    public static <T> T getBean(String groupName,String alias){
        return (T)SpringBeanUtils.getBean(createAliasBeanName(groupName,alias));
    }

    /**
     * @author hillchen
     * Description: 根据别名信息获取springbean
     * Date 2023/8/8 23:21
     * @param <T> springbean 类型
     * @param alias springbean别名
     * @param clazz springbean类型
     * @return T
     **/
    public static <T> T getBean(String alias,Class<T> clazz){
        return SpringBeanUtils.getBean(createAliasBeanName(DEF_GROUP_NAME,alias),clazz);
    }

    /**
     * @author hillchen
     * Description: 根据别名信息获取springbean
     * Date 2023/8/8 23:21
     * @param <T> springbean 类型
     * @param alias springbean别名
     * @return T
     **/
    public static <T> T getBean(String alias){
        return (T)SpringBeanUtils.getBean(createAliasBeanName(DEF_GROUP_NAME,alias));
    }
}
